package silo.dungeonbuddy;

import java.io.*;
import java.net.*;

/**NOT USING THIS CLASS ANYMORE*/

class Server
{
	//private String outMessage = "DEFAULT";
	private String inMessage = "DEFAULT";
	private Start game;
	
	public Server(Start eGame)
	{
		game = eGame;
	}
	
	public void listen()
	{
		//String clientSentence;
        //String capitalizedSentence;
        try
        {
        	ServerSocket welcomeSocket = new ServerSocket(12375);
        	
        	//while(true)
            //{
            	//try
            	//{
        			welcomeSocket.setSoTimeout(500);
            		Socket connectionSocket = welcomeSocket.accept();
            		BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                    inMessage = inFromClient.readLine();
                    System.out.println("Received: " + inMessage);
                    outToClient.writeBytes(determineOutputMessage() + '\n');
            	//}catch(Exception e)
            	//{
            		
            	//}//catch
            	//try
    			//{
    				//Thread.sleep(10);
    			//}
    			//catch (Exception e)
    			//{
    				
    			//}
            //}//loop
        }catch(Exception e)
        {
        	
        }//catch
   }//main
	
	public String determineOutputMessage()
	{
		if(inMessage.contains("!MAP"))
		{
			return game.getBoard().toString();
		}else if(inMessage.contains("!UPDATE"))
		{
			try
			{
				String s = "";
				String IP = "";
				int k = 0;
				while(inMessage.charAt(k)!=',')
				{
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					IP = IP + inMessage.charAt(k);
					k = k + 1;
				}//loop
				
				
				String x="";
				String y="";
				String spriteRef="";
				String sizeX="";
				String sizeY="";
				String visible="";
				//String IP;
				//private int squareIndex;
				String row="";
				String column="";
				String speed="";
				String movement="";
				String stealth="";
				String perception="";
				String initiative="";
				String dex="";
				String turn="";
				
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					x=x+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					y=y+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					spriteRef=spriteRef+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					sizeX=sizeX+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					sizeY=sizeY+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					visible=visible+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				//IP
				while(inMessage.charAt(k)!=',')
				{
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					row=row+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					column=column+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					speed=speed+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					movement=movement+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					stealth=stealth+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					perception=perception+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					initiative=initiative+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					dex=dex+inMessage.charAt(k);
					k = k + 1;
				}//loop
				k = k + 1;
				while(inMessage.charAt(k)!=',')
				{
					turn=turn+inMessage.charAt(k);
					k = k + 1;
				}//loop
				//Not using this class anymore, but an error was annoying me - that's the reason for the edit.
				Unit u = new Unit("Unit : " + IP,game,spriteRef,IP,Integer.parseInt(row),Integer.parseInt(column),Integer.parseInt(sizeX),Integer.parseInt(sizeY),0);
				for(int i=0;i<game.getUnitList().size();i++)
				{
					try
					{
						Unit u2 = (Unit)game.getUnitList().get(i);
						//Changed this b/c there was an error, but this class should not be used anymore
						//Instead, a multi-threaded server is used.
						//I should just delete this class but *shrug*
						if(u2.getOwner().equalsIgnoreCase(IP))
						{
							game.removeUnit(u2);
							game.addUnit(u);
							i=game.getUnitList().size();
							break;
						}
					}catch(Exception e)
					{
						
					}//catch
				}//next i
				s = game.getUnitList().size() + ",";
				for(int i=0; i<game.getUnitList().size();i++)
				{
					s = s + game.getUnitList().get(i).toString() + ",";
				}//next i
				return s;
			}catch(Exception e)
			{
				return "DEFAULT";
			}//catch
		}
		return "DEFAULT";
	}//determineOutputMessage()
	
	//GETTERS
	//public String getOutMessage()
	//{
		//return outMessage;
	//}
	public String getInMessage()
	{
		return inMessage;
	}
	
	//SETTERS
	public void setInMessage(String eMess)
	{
		inMessage = eMess;
	}
	//public void setOutMessage(String eMess)
	//{
		//outMessage = eMess;
	//}
}//class Server